var express = require("express");
const { connection } = require("../model/db"); // 获取连接实例
const router = express.Router();

// 示例
// router.get("/msglist", (req, res) => {
//     connection.query(`select * from user;`, (err, data) => {
//         if (err) {
//             console.log("err", err)
//             res.send('query error')
//         } else {
//             // 将 MySQL 查询结果作为路由返回值
//             res.send({
//                 code: 200,
//                 msg: "获取数据成功",
//                 data
//             })
//         }
//     })
// })

// 查看选题规则
router.get("/rulelist", (req, res) => {
  // 获取出题方id
  let select_id = req.query.selectId
  let sql = "select s.*,d.dept_name as select_name,p.dept_name as answer_name from select_rule s join department d join department p on s.select_id=d.id and s.answer_id=p.id;"
  if(select_id){
    sql = `select s.*,d.dept_name as select_name,p.dept_name as answer_name from select_rule s join department d join department p on s.select_id=d.id and s.answer_id=p.id where s.select_id='${select_id}';`
  }
  connection.query(`${sql}`, (err, data) => {
    if (err) {
      console.log("err", err);
      res.send({
        code: 400,
        msg: "query error",
        err
      });
    } else {
      res.send({
        code: 200,
        msg: "获取数据成功",
        data,
      });
    }
  });
})

// 添加选题规则
router.post("/Addselect_rule", (req, res) => {
  let body = req.body;
  let sql = `INSERT INTO select_rule(answer_id,select_id) VALUES(${body.answer_id},${body.select_id});`;
  connection.query(sql, (err, data) => {
    if (err) {
      console.log("query err");
      res.send("query err");
    } else {
      res.send({
        code: 200,
        msg: "添加成功",
        data,
      });
    }
  });
});
// 删除选题规则
router.delete('/Delselect_rule/:id', (req, res) => {
  let id = req.params.id;
  let sql = `DELETE FROM select_rule WHERE id=${id};`
  connection.query(sql, (err, data) => {
    if (err) {
      console.log('query err');
      res.send('query err')
    } else {
      res.send({
        msg: "删除成功",
        code: 200,
        data
      })
    }
  })
})
// 学院部门管理接口 增删改查
// 获取学院部门
router.get("/Getdepartment", (req, res) => {
  let sql = `select * from department;`;
  connection.query(sql, (err, data) => {
    if (err) {
      console.log("error");
      res.send("query error");
    } else {
      res.send({
        code: 200,
        msg: "获取数据成功",
        data,
      });
    }
  });
});
// 增加学院部门
router.post("/Adddepartment", (req, res) => {
  let body = req.body;
  console.log(body);
  let sql = `INSERT INTO department(dept_name,is_college) VALUES('${body.dept_name}',${body.is_college}) `;
  console.log(sql);
  connection.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.send("query error");
    } else {
      res.send({
        code: 200,
        msg: "添加部门成功",
        data,
      });
    }
  });
});
// 删除部门
router.delete("/Deldepartment/:id", (req, res) => {
  let id = req.params.id;
  let sql = `DELETE FROM department WHERE id=${id};`;
  connection.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.send("query error");
    } else {
      res.send({
        code: 200,
        msg: "删除部门成功",
        data,
      });
    }
  });
});
// 修改部门
// 获取单个部门信息
router.get("/Getdepartmentone", (req, res) => {
  let id = req.query.id;
  let sql = `SELECT * FROM department WHERE id=${id};`;
  connection.query(sql, (err, data) => {
    if (err) {
      console.log("error");
      res.send("query error");
    } else {
      res.send({
        code: 200,
        msg: "查询成功",
        data: data[0],
      });
    }
  });
});
// 修改部门接口
router.put("/editdepartment", (req, res) => {
  let body = req.body;
  let sql = `UPDATE department SET dept_name='${body.dept_name}',is_college=${body.is_college} WHERE id=${body.id}`;
  connection.query(sql, (err, data) => {
    if (err) {
      console.log("query err");
      res.send("err");
    } else {
      res.send({
        code: 200,
        msg: "修改成功",
        data,
      });
    }
  });
  res.send({
    body,
  });
});
module.exports = router;
